﻿using QFramework;
using System.Linq;
using UnityEngine;

namespace ProjectIndieFarm
{
    public class AddItemCountCommand : AbstractCommand<bool>
    {
        private readonly string mItemName;
        private readonly int mAddCount;
        public AddItemCountCommand(string itemName, int addCount)
        {
            mItemName = itemName;
            mAddCount = addCount;
        }
        protected override bool OnExecute()
        {
            var toolBarSystem = this.GetSystem<IToolbarSystem>();
            //在道具栏中查找 数量增加的物体
            var slot = toolBarSystem.Slots
                .FirstOrDefault(slot => slot.ItemID.Value == mItemName);
            //如果不在道具栏
            if (slot == null)
            {
                //将物体放进道具栏
                slot = toolBarSystem.Slots
                    .FirstOrDefault(slot => slot.Count.Value == 0);
                if (slot == null) { return false; }
                slot.ItemID.Value = mItemName;
                slot.Count.Value = mAddCount;
            }
            else { slot.Count.Value += mAddCount; }
            ToolbarSystem.OnItemCountChange.Trigger(slot, slot.Count.Value);
            return true;
        }
    }
}
